﻿#define _CRT_SECURE_NO_WARNINGS
#include<assert.h>
#include<iostream>
#include<string>
using namespace std;

namespace bit
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _next;
		ListNode<T>* _prev;
		T _data;
		
		//一个参数的
		ListNode(T&& data)
			:_next(nullptr)
			, _prev(nullptr)
			, _data(move(data))
		{}

		//参数包的
		template <class... Args>
		ListNode(Args&&... args)
			: _next(nullptr)
			, _prev(nullptr)
			, _data(std::forward<Args>(args)...)
		{}
	};

	template<class T, class Ref, class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;
		Node* _node;
		ListIterator(Node* node)
			:_node(node)
		{}

		// ++it;
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Ref operator*()
		{
			return _node->_data;
		}

		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}
	};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;
	public:
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T*> const_iterator;

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		void empty_init()
		{
			_head = new Node();
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			empty_init();
		}

		//左值
		void push_back(const T& x)
		{
			insert(end(), x);
		}

		//右值
		void push_back(T&& x)
		{
			insert(end(), move(x));
		}

/*
	万能引用    适配左值和右值
		template<class X>
		void push_back(X&& x)
		{
			insert(end(),forward<X>(x));
		}
*/

		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* newnode = new Node(x);
			Node* prev = cur->_prev;
			// prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			return iterator(newnode);
		}

		iterator insert(iterator pos, T&& x)
		{
			Node* cur = pos._node;
			Node* newnode = new Node(move(x));
			Node* prev = cur->_prev;
			// prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			return iterator(newnode);
		}

/*
  这样就不会支持隐式类型转换了
	template<class X>
	iterator insert(iterator pos,X&& x)
	{
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* newnode = new Node(forward<X>(x));
	
		newnode->_next =cur;
		cur->_prev =newnode;
		newnode->_prev = prev;
		prev ->_next =newnode;
		
		++_size;
	}

*/

		//不一定需要包的扩展   但是需要完美转发
		template <class... Args>
		void emplace_back(Args&&... args)
		{
			insert(end(), std::forward<Args>(args)...);    //这里的完美转发需要加 ...
		}
			// 原理：本质编译器根据可变参数模板⽣成对应参数的函数 
			/*void emplace_back(string& s)
			{
				insert(end(), std::forward<string>(s));
			}
			
			void emplace_back(string&& s)
			{
				insert(end(), std::forward<string>(s));
			}
			
			void emplace_back(const char* s)
			{
				insert(end(), std::forward<const char*>(s));
			}
			*/
	
	
		template <class... Args>
		iterator insert(iterator pos, Args&&... args)
		{
			Node* cur = pos._node;
			Node* newnode = new Node(std::forward<Args>(args)...);
			Node* prev = cur->_prev;
			// prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			return iterator(newnode);
		}
	private:
		Node* _head;
	};
}



namespace lcx
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		//构造
		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			cout << "string(char* str) -- 构造" << endl;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		//交换函数
		void swap(string& s)
		{
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		}


		//拷贝构造
		string(const string& s)
			//:_str(nullptr)
		{
			cout << "string(const string& s)---- 拷贝构造" << endl;
			//扩容  拷贝
			reserve(s._capacity);
			for (auto ch : s)
			{
				push_back(ch);
			}
		}

		//移动构造
		string(string&& s)
		{
			cout << "string(string&& s) --- 移动构造" << endl;
			swap(s);
		}

		//拷贝赋值
		string& operator=(const string& s)
		{
			cout << "string& operator=(const string& s) -- 拷贝赋值" << endl;

			if (this != &s)
			{
				_str[0] = '\0';
				_size = 0;

				reserve(s._capacity);
				for (auto ch : s)
				{
					push_back(ch);
				}
			}
			return *this;
		}

		//移动赋值
		string& operator=(string&& s)   //这里的移动赋值也要传右值
		{
			cout << "string& operator=(string&& s) -- 移动赋值" << endl;
			swap(s);
			return *this;
		}

		//析构
		~string()
		{
			//cout << "~string() -- 析构" << endl;
			delete[] _str;
			_str = nullptr;
		}

		//赋值重载
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		//扩容函数
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				if (_str)
				{
					strcpy(tmp, _str);
					delete[] _str;
				}
				_str = tmp;
				_capacity = n;
			}
		}

		//压栈尾插函数
		void push_back(char ch)
		{
			if (_size >= _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}


		// += 操作
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		//对应 字符串
		const char* c_str() const
		{
			return _str;
		}

		//返回大小
		size_t size() const
		{
			return _size;
		}

	private:						
		char* _str = new char('\0');  //这个是防止  cpp文件中的_name是空串的时候   是空串就会有问题   这里提前给上一个字符  防止出现问题
		int _size = 0;
		int _capacity = 0;
	};
}